package com.szp.lg.blog.sercvice.impl;

import com.szp.lg.blog.dao.BlogDao;
import com.szp.lg.blog.pojo.Blog;
import com.szp.lg.blog.sercvice.BlogService;
import com.szp.lg.blog.util.PageQueryUtil;
import com.szp.lg.blog.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    BlogDao blogDao;

    @Override
    public PageResult findAll(PageQueryUtil pageQueryUtil) {
        PageResult pageResult = new PageResult();
        PageRequest pageRequest = PageRequest.of(pageQueryUtil.getPage()-1, pageQueryUtil.getLimit());
        Page page =  blogDao.findAll(pageRequest);
        pageResult.setList(page.getContent());
        pageResult.setCurrPage(page.getNumber() + 1);
        pageResult.setPageSize(page.getSize());
        pageResult.setTotalCount((int)page.getTotalElements());
        pageResult.setTotalPage(page.getTotalPages());
        return pageResult;
    }
}
